*------------------------------------*
* Party Strength and Economic Growth *
*------------------------------------*

*----------------
* Data preparation Codes
*----------------

*--------
* Loading the data

* Load V-Dem data
use "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\vdem_6.2.dta", clear

* Keep only variables that will be used
keep country_name country_text_id country_id year COWcode ///
 v2psorgs v2psprlnks v2psprbrch v2pscohesv v2pscnslnl v2psswitch v2pscomprg v2xps_party ///
 v2psorgs_codelow v2psprlnks_codelow v2psprbrch_codelow v2pscohesv_codelow v2pscnslnl_codelow v2psswitch_codelow v2pscomprg_codelow ///
 v2psorgs_codehigh v2psprlnks_codehigh v2psprbrch_codehigh v2pscohesv_codehigh v2pscnslnl_codehigh v2psswitch_codehigh v2pscomprg_codehigh ///
 e_migdpgro e_migdppcln e_miurbani e_pelifeex e_Total_Oil_Income_PC e_miinterc e_miinteco e_regionpol e_polity2 e_boix_regime ///
 v2x_polyarchy v2xcl_prpty v2x_jucon v2xlg_legcon v2x_corr v2clstown v2xcs_ccsi ///
 v2clrspct v2cltrnslw v2xcl_acjst v2cltort v2clkill v2xcl_slave v2clrelig v2clfmove v2xcl_dmove ///
 e_wbgi_cce e_wbgi_gee e_wbgi_pse e_wbgi_rle e_wbgi_rqe e_wbgi_vae ///
 e_miinflat e_peinfmor v2elparlel
 
* Merge variables not in dataset
merge 1:1 country_id year using "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\other_external.dta", nogenerate

* Drop countries not on V-Dem 
replace e_regionpol = e_regionpol[_n-1] if e_regionpol == . & country_id == country_id[_n-1]
keep if e_regionpol != .

* Save data
* save "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\ps_data.dta", replace

*--------
* Creating new variables
xtset country_id year 

* Generate party strength index
gen v2psswitch_neg = -1 * v2psswitch
egen v2psswitch_neg_std = std(v2psswitch_neg)
egen v2pscohesv_std = std(v2pscohesv)
gen v2pscnslnl_neg = -1 * v2pscnslnl
egen v2pscnslnl_neg_std = std(v2pscnslnl_neg)
egen v2psprlnks_std = std(v2psprlnks)
egen v2psorgs_std = std(v2psorgs)
egen v2psprbrch_std = std(v2psprbrch)
gen ps = (v2pscohesv_std + v2psswitch_neg_std + v2pscnslnl_neg_std + v2psprlnks_std + v2psorgs_std + v2psprbrch_std)/6 

label variable ps "Party Strength"
label variable v2psswitch_neg_std "Party switching"
label variable v2pscohesv_std "Legislative cohesion"
label variable v2pscnslnl_neg_std "National candidate selection"
label variable v2psprlnks_std "Programmatic linkages"
label variable v2psorgs_std "National organizations"
label variable v2psprbrch_std "Party branches"


* Generate 5 year averages
local vars5y e_migdppcln e_migdpgro ps
foreach var of local vars5y {
by country_id: generate `var'_5yr = (`var' + L.`var' + L2.`var' + L3.`var' + L4.`var') / 5
replace `var'_5yr = . if year != 2015 &year != 2010 & year != 2005 & year != 2000 & year != 1995 & year != 1990 & year != 1985 & year != 1980 & year != 1975 & year != 1970 & year != 1965 & year != 1960 & year != 1955 & year != 1950 & year != 1945 & year != 1940 & year != 1935 & year != 1930 & year != 1925 & year != 1920 & year != 1915 & year != 1910 & year != 1905 & year != 1900
}

label variable e_migdppcln_5yr "GDP per capita (5yrs)"
label variable e_migdpgro_5yr "GDP growth (5yrs)" 
label variable ps_5yr "Party Strength (5yrs)"

* Stock variable
gen ps_0 = ps + 1.692
gen ps_stock = ps + 1.692
by country_id: gen trend = _n
replace ps_stock = ps_stock[_n-1]*0.99 + ps_0 if country_id == country_id[_n-1] & trend != 1
label variable ps_stock "Party Strength (Stock)"
drop ps_0

* Ordinal variable
gen ps_ord = 0 if ps < -1.0693694 & ps !=.
replace ps_ord = 1 if ps > -1.0693694 & ps < -.4467818 & ps !=.
replace ps_ord = 2 if ps > -.4467818 & ps < .1758058 & ps !=.
replace ps_ord = 3 if ps > .1758058 & ps < .7983934 & ps !=.
replace ps_ord = 4 if ps > .7983934 & ps !=.

* PCA
pca v2pscohesv_std v2psswitch_neg_std v2pscnslnl_neg_std v2psprlnks_std v2psorgs_std v2psprbrch_std
		predict ps_pca
		label variable ps_pca "Party Strength (PCA)" 

* Multiplicative
gen ps_mult = v2pscohesv_std * v2psswitch_neg_std * v2pscnslnl_neg_std * v2psprlnks_std * v2psorgs_std * v2psprbrch_std

* No PSwitch
gen ps_1 = (v2pscohesv_std + v2pscnslnl_neg_std + v2psprlnks_std + v2psorgs_std + v2psprbrch_std)/5
label variable ps_1 "No Switching"

* No Cohesion
gen ps_2 = (v2psswitch_neg_std + v2pscnslnl_neg_std + v2psprlnks_std + v2psorgs_std + v2psprbrch_std)/5
label variable ps_2 "No Cohesion"

* No Candidate Selection
gen ps_3 = (v2pscohesv_std + v2psswitch_neg_std + v2psprlnks_std + v2psorgs_std + v2psprbrch_std)/5
label variable ps_3 "No Candidate Selection"

* No Linkages
gen ps_4 = (v2pscohesv_std + v2psswitch_neg_std + v2pscnslnl_neg_std + v2psorgs_std + v2psprbrch_std)/5
label variable ps_4 "No Linkages"

* No Orgs
gen ps_5 = (v2pscohesv_std + v2psswitch_neg_std + v2pscnslnl_neg_std + v2psprlnks_std + v2psprbrch_std)/5
label variable ps_5 "No Organizations"

* No Branches
gen ps_6 = (v2pscohesv_std + v2psswitch_neg_std + v2pscnslnl_neg_std + v2psprlnks_std + v2psorgs_std + v2psprbrch_std)/6 
label variable ps_6 "No Branches"

* With Nationalization
egen v2pscomprg_std = std(v2pscomprg)
gen ps_7 = (v2pscohesv_std + v2psswitch_neg_std + v2pscnslnl_neg_std + v2psprlnks_std + v2psorgs_std + v2psprbrch_std + v2pscomprg_std)/7 
label variable ps_7 "With Nationalization"

* Only structural variables 
gen ps_8 = (v2pscnslnl_neg_std + v2psorgs_std + v2psprbrch_std)/3
label variable ps_8 "Structural Variables"

* Regional dummies
tabulate e_regionpol, generate(region)
rename region1 easteur
label variable easteur "Eastern Europe, Central Asia, Post-Communist"
rename region3 mena
label variable mena "MENA"
rename region4 ssafrica
label variable ssafrica "Sub-Saharan Africa"
rename region5 west
label variable west "West"
gen asia = 0
replace asia = 1 if region6 == 1 |region7 == 1 | region8 == 1 | region9 == 1
label variable asia "Asia and Pacific"
gen latam = 0
replace latam = 1 if region2 == 1 | region10 == 1
label variable latam "Latin America"
drop region*

* Prepare for regional averages
egen WER=sum(ps) if  west==1, by (year)
egen WE = sum(west) if  west==1 & ps!=., by (year)
generate WERA = (WER-ps)/(WE-1)

egen EER=sum(ps) if  easteur==1, by (year)
egen EE = sum(easteur) if  easteur==1 & ps!=., by (year)
generate EERA = (EER-ps)/(EE-1)

egen AFR=sum(ps) if  ssafrica==1, by (year)
egen AF = sum(ssafrica) if  ssafrica==1 & ps!=., by (year)
generate AFRA = (AFR-ps)/(AF-1)

egen ASR=sum(ps) if  asia==1, by (year)
egen AS = sum(asia) if  asia==1 & ps!=., by (year)
generate ASRA = (ASR-ps)/(AS-1)

egen LAR=sum(ps) if  latam==1, by (year)
egen LA = sum(latam) if  latam==1 & ps!=., by (year)
generate LARA = (LAR-ps)/(LA-1)

egen MER=sum(ps) if  mena==1, by (year)
egen ME = sum(mena) if  mena==1 & ps!=., by (year)
generate MERA = (MER-ps)/(ME-1)

* Regional averages, excepting country in question
generate regionps_minus = WERA
replace regionps_minus = EERA if regionps_minus==.
replace regionps_minus = AFRA if regionps_minus==.
replace regionps_minus = ASRA if regionps_minus==.
replace regionps_minus = MERA if regionps_minus==.
replace regionps_minus = LARA if regionps_minus==.

* Pure regional averages
egen westps=mean(ps) if  west==1, by (year)
egen easteurps=mean(ps) if  easteur==1, by (year)
egen africaps=mean(ps) if  ssafrica==1, by (year)
egen asiaps=mean(ps) if  asia==1, by (year)
egen menaps=mean(ps) if  mena==1, by (year)
egen latamps=mean(ps) if  latam==1, by (year)

generate regionps = westps
replace regionps = easteurps if regionps==.
replace regionps = africaps if regionps==.
replace regionps = asiaps if regionps==.
replace regionps = menaps if regionps==.
replace regionps = latamps if regionps==.

* Generate global average
egen globalps=mean(ps), by (year)
egen totalglobalps=sum(ps), by (year)

* Generate global average, minus country in question
egen globcount = sum(west+ easteur+ ssafrica + asia+ latam+ mena) if ps!=., by (year)
generate globalps_minus = (totalglobalps-ps)/(globcount-1)

* Generate regional and global GROWTH averages, minus country in question
egen WERg=sum(e_migdpgro) if  west==1, by (year)
egen WEg = sum(west) if  west==1 & e_migdpgro!=., by (year)
generate WERAg = (WERg-e_migdpgro)/(WEg-1)

egen EERg=sum(e_migdpgro) if  easteur==1, by (year)
egen EEg = sum(easteur) if  easteur==1 & e_migdpgro!=., by (year)
generate EERAg = (EERg-e_migdpgro)/(EEg-1)

egen AFRg=sum(e_migdpgro) if  ssafrica==1, by (year)
egen AFg = sum(ssafrica) if  ssafrica==1 & e_migdpgro!=., by (year)
generate AFRAg = (AFRg-e_migdpgro)/(AFg-1)

egen ASRg=sum(e_migdpgro) if  asia==1, by (year)
egen ASg = sum(asia) if  asia==1 & e_migdpgro!=., by (year)
generate ASRAg = (ASRg-e_migdpgro)/(ASg-1)

egen LARg=sum(e_migdpgro) if  latam==1, by (year)
egen LAg = sum(latam) if  latam==1 & e_migdpgro!=., by (year)
generate LARAg = (LARg-e_migdpgro)/(LAg-1)

egen MERg=sum(e_migdpgro) if  mena==1, by (year)
egen MEg = sum(mena) if  mena==1 & e_migdpgro!=., by (year)
generate MERAg = (MERg-e_migdpgro)/(MEg-1)

*gerenate regional averages, excepting country in question
generate regiongrowth_minus = WERAg
replace regiongrowth_minus = EERAg if regiongrowth_minus==.
replace regiongrowth_minus = AFRAg if regiongrowth_minus==.
replace regiongrowth_minus = ASRAg if regiongrowth_minus==.
replace regiongrowth_minus = MERAg if regiongrowth_minus==.
replace regiongrowth_minus = LARAg if regiongrowth_minus==.

**generate global average growth minus country in question
**generate global PI average
egen globcountg = sum(west + easteur + ssafrica + asia + latam + mena) if e_migdpgro!=., by (year)
egen globalgrowth = mean(e_migdpgro) if e_migdpgro!=., by (year)
egen totalglobalgrowth=sum(e_migdpgro), by (year)
generate globalgrowth_minus = (totalglobalgrowth-e_migdpgro)/(globcountg-1)

* Drop variables that won't be used
drop WER WE WERA EER EE EERA AFR AF AFRA ASR AS ASRA LAR LA LARA MER ME MERA westps easteurps africaps asiaps menaps latamps regionps totalglobalps globcount ///
WERg WEg WERAg EERg EEg EERAg AFRg AFg AFRAg ASRg ASg ASRAg LARg LAg LARAg MERg MEg MERAg globcountg totalglobalgrowth

* Label variables 
label variable regionps_minus "Regional PS average, minus country in question"
label variable globalps "Global PS average"
label variable globalps_minus "Global PS average, minus country in question"
label variable regiongrowth_minus "Regional growth average, minus country in question"
label variable globalgrowth "Global growth average"
label variable globalgrowth_minus "Global growth average, minus country in question"
* Generate new indices using v-dem data:
	* Individual Rights (freedoms)
		pca v2xcl_acjst v2cltort v2clkill v2xcl_slave v2clrelig v2clfmove v2xcl_dmove
		predict liberties
		label variable liberties "Civil Liberties" 
	* Public Administration
		pca v2clrspct v2cltrnslw
		predict law
		label variable law "Public Administration"
		
* Add new variables for state capacity
merge 1:1 country_name year using "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\statehist_chk.dta", nogenerate
drop if country_id == .
replace statehist5 = statehist5[_n-1] if statehist5==. & country_name == country_name[_n-1]

merge 1:1 country_text_id year using "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\wdi_taxrevenue.dta", nogenerate
drop if country_id == .

* Rename variable 
label variable e_migdppcln "GDP per capita"
label variable wdi_taxrevenue "Tax Revenue (WDI)"

* Add Hanson Sigman State Capacity Data
rename COWcode ccode
merge m:1 ccode year using "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\State Capacity Dataset v.9.dta", nogenerate
drop country cntrynum isocode
drop if country_id == .

* Parliamentarism
do "C:\Users\fbizz\Dropbox\Party strength\Empirics\Codes\parliamentarism.do"

rename partyage partyage_local
merge 1:1 country_name year using "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\dpi_partyage.dta", assert(match) keep(master) nogenerate

* Merge Penn World Tables data
merge 1:1 country_name year using "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\pwt.dta", assert(match) keep(master) nogenerate

*--------
* Saving
save "C:\Users\fbizz\Dropbox\Party strength\Empirics\Data\ps_data.dta", replace

